<script setup>
import { onMounted, ref } from 'vue'
import dial from './dial.vue'
import { getAllApi, delApi } from '@/api/warehousingentry/warehousingEntry'
import { getInfoApi } from '@/api/warehouse/warehouse'
import { ElMessage } from 'element-plus'

const tableData = ref([])
const total = ref(0)
const currentPage = ref(1)
const pageSize = ref(10)
const size = ref('default')
const background = ref(true)
const disabled = ref(false)
const warehouseList = ref([])
const getWarehouseList = async () => {
  let res = await getInfoApi()
  warehouseList.value = res
}
const getWarehouseNameById = (warehouseId) => {
  const warehouse = warehouseList.value.find(item => item.id === warehouseId)
  return warehouse ? warehouse.name : '未知仓库'
}
const handleSizeChange = (val) => {
  pageSize.value = val
  getAll()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  getAll()
}

const form = ref({
  batchNumber: '',
  consignorId: null
})

const statusMap = {
  1: '未确认',
  2: '部分已确认',
  3: '全部确认',
  4: '拣货中',
  5: '已上架'
}

const getAll = async () => {
  let res = await getAllApi(
    currentPage.value,
    pageSize.value
  )
  total.value = res.total
  tableData.value = res.rows
}

const diaRef = ref(null)
const handleAdd = () => {
  diaRef.value.dialogVisible = true
}

const handleEdit = (row) => {
  diaRef.value.dialogFormData = { ...row }
  diaRef.value.dialogVisible = true
}

const search = () => {
  currentPage.value = 1
  getAll()
}

const ids = ref([])
const handleSelectionChange = (val) => {
  ids.value = val.map(item => item.id)
}

const delById = (id) => {
  ids.value = [id]
  delByIds()
}

const delByIds = async () => {
  const res = await delApi(ids.value)
  if (res) {
    ElMessage.success('删除成功!')
    getAll()
  } else {
    ElMessage.error('删除失败！')
  }
}

defineExpose({
  refreshData: getAll
})

onMounted(() => {
  getAll()
  getWarehouseList()
})
</script>

<template>
  <dial ref="diaRef" @refresh="getAll" :warehouse-list="warehouseList" />
  <div>
    <el-button @click="handleAdd" type="primary" style="float: left;">新增</el-button>
  </div>
  <el-form :model="form" :inline="true" style="float: right;">
    <el-form-item>
      <el-input v-model="form.batchNumber" placeholder="批次号" />
    </el-form-item>
    <el-form-item>
      <el-input v-model="form.consignorId" placeholder="货主ID" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="search">查询</el-button>
    </el-form-item>
  </el-form>

  <el-table :data="tableData" style="width: 100%" @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="55" />
    <el-table-column property="batchNumber" label="批次号" />
    <el-table-column property="totalCount" label="商品数量" />
    <el-table-column property="predictedArrivalTime" label="预测到达时间" />
    <el-table-column property="warehouseId" label="仓库ID">
      <template #default="{ row }">
        {{ getWarehouseNameById(row.warehouseId) }}
      </template>
    </el-table-column>
    <el-table-column property="status" label="状态">
      <template #default="{ row }">{{ statusMap[row.status] }}</template>
    </el-table-column>
    <el-table-column property="createTime" label="创建时间" />
    <el-table-column fixed="right" label="操作" width="250">
      <template #default="{ row }">
        <el-button type="primary" @click="handleEdit(row)">编辑</el-button>
        <el-button type="danger" @click="delById(row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <div class="demo-pagination-block">
    <el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="[10, 20, 30, 40]"
      :size="size" :disabled="disabled" :background="background" layout="total, sizes, prev, pager, next, jumper"
      :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </div>
</template>